package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApReadBehaviorMapper;
import com.heima.behavior.service.ApBehaviorService;
import com.heima.behavior.service.ApReadBehaviorService;
import com.heima.common.constans.message.MessageConstants;
import com.heima.model.behavior.dto.ReadBehaviorDto;
import com.heima.model.behavior.pojo.ApBehaviorEntry;
import com.heima.model.behavior.pojo.ApReadBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.ApUserThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class ApReadBehaviorServiceImpl extends ServiceImpl<ApReadBehaviorMapper, ApReadBehavior> implements ApReadBehaviorService {


    @Autowired
    ApBehaviorService behaviorService;

    /**
     * 保存用户阅读行为
     * @param dto
     * @return
     */
    @Override
    public ResponseResult read(ReadBehaviorDto dto) {
        //检查数据
        if (dto==null||dto.getArticleId()==null||dto.getCount()==null||dto.getLoadDuration()==null||dto.getPercentage()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser user = ApUserThreadLocalUtils.getUser();
        if (dto.getEquipmentId()==null){
            dto.setEquipmentId(user.getId());
        }
        //查询行为对象
        ApBehaviorEntry behaviorEntry = behaviorService.getOne(Wrappers.<ApBehaviorEntry>lambdaQuery().eq(ApBehaviorEntry::getEntryId, dto.getEquipmentId()));
        if (behaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询阅读行为，如果存在则修改，不存在则创建
        ApReadBehavior readBehavior = getOne(Wrappers.<ApReadBehavior>lambdaQuery().eq(ApReadBehavior::getArticleId, dto.getArticleId()).eq(ApReadBehavior::getEntryId, dto.getEquipmentId()));
        if (readBehavior==null){
            readBehavior=new ApReadBehavior();
            readBehavior.setArticleId(dto.getArticleId());

            readBehavior.setCreatedTime(new Date());

            readBehavior.setEntryId(behaviorEntry.getId());

        }
        readBehavior.setCount((short)(readBehavior.getCount()+1));
        readBehavior.setReadDuration(dto.getReadDuration()+readBehavior.getReadDuration());
        readBehavior.setPercentage(dto.getPercentage());
        readBehavior.setLoadDuration(dto.getLoadDuration());
        readBehavior.setUpdatedTime(new Date());
        saveOrUpdate(readBehavior);
        sendmessage(dto,1);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Autowired
    KafkaTemplate kafkaTemplate;
    //发送消息进行实时流更新
    private void sendmessage(ReadBehaviorDto dto, int i) {
        UpdateArticleMess mess=new UpdateArticleMess();
        mess.setAdd(i);
        mess.setArticleId(dto.getArticleId());
        mess.setType(UpdateArticleMess.UpdateArticleType.VIEWS);
        kafkaTemplate.send(MessageConstants.HOT_ARTICLE_BEHAVIOR_SEND_TOPIC, JSON.toJSONString(mess));
    }
}
